extends Node2D

onready var Sunlabel = get_node("Sun/Label")
onready var SunFlower = load("res://object/SunFlower.tscn")
onready var WanDou = load("res://object/Wandou.tscn")
onready var JianGuo = load("res://object/Jinweijianguo.tscn")
# hover对象
var selected_plant

var selected_Object = {
	"name":"",
	"cost":0
	}
# 重构用
enum plant_name {sunflower,wandou,jinweijianguo}
# 铲子
var use_shovel = false
var nengliangdou = 4
var use_nld = false

func _ready():
	randomize()
	#connect_places()
	pass 

func _on_SunTimer_timeout():
	var rx = rand_range(0,1024)
	var ry = rand_range(0,600)
	var Sun = load("res://object/sun.tscn").instance()
	Sun.rect_position = Vector2(rx,ry)
	add_child(Sun)
	pass 
	
#func _process(delta):
#
#	pass
	
func _input(event):
	if event is InputEventMouseButton:
		if event.is_pressed() and event.button_index == BUTTON_RIGHT:
			if is_instance_valid(selected_plant):
				selected_plant.queue_free()
	pass
# 向日葵
func _on_TextureButton_pressed():
	if Sunlabel.number >=50:
		$Choosecard.play()
		var card1_hover = load("res://object/PlantHover.tscn").instance()
		add_child(card1_hover)
		selected_plant = card1_hover
		selected_Object.name = "sunflower"
		selected_Object.cost = 50
	else:
		pass
	pass
# 重构 5个僵尸的起始位置
var p1 = Vector2(909,140)
var p2 = Vector2(909,220)
var p3 = Vector2(909,330)
var p4 = Vector2(909,440)
var p5 = Vector2(909,550)

func zombie():
	var zombie = load("res://object/Zombie.tscn").instance()
	var idx = rand_range(1,6)
	if int(idx) == 1:
		zombie.position = p1
	elif int(idx) == 2:
		zombie.position = p2
	elif int(idx) == 3:
		zombie.position = p3
	elif int(idx) == 4:
		zombie.position = p4
	elif int(idx) == 5:
		zombie.position = p5
	add_child(zombie)
	
func _on_zombieTimer_timeout():
	zombie()
	pass # Replace with function body.

func connect_places():
	for place in get_node("places").get_children():
		connect("input_event",place,"_on_place_input_event")
	pass

# 法术豌豆 使用法术伤害公式
func _on_TextureButton2_pressed():
	if Sunlabel.number >= 100:
		$Choosecard.play()
		var card2_hover = load("res://object/PlantHover.tscn").instance()
		card2_hover.texture = load("res://assets/sprite/Plants/Peashooter/Peashooter_0.png")
		add_child(card2_hover)
		selected_plant = card2_hover
		selected_Object.name = "wandou"
		selected_Object.cost = 100
	pass
	
func reset_shovel():
	$Shovel.move_with_mouse = false
	$Shovel/sprite.position = Vector2(41,25)

# 近卫坚果
func _on_TextureButton3_pressed():
	if Sunlabel.number >= 50:
		$Choosecard.play()
		var card2_hover = load("res://object/PlantHover.tscn").instance()
		card2_hover.texture = load("res://assets/sprite/Plants/WallNut/jinweiwall/wall0000.png")
		add_child(card2_hover)
		selected_plant = card2_hover
		selected_Object.name = "jinweijianguo"
		selected_Object.cost = 50
	pass

func _on_MenuButton_pressed():
	$Panel.show()
	get_tree().paused = true
	pass

func _on_back_pressed():
	get_tree().paused = false
	$Panel.hide()
	pass

func _on_home_pressed():
	get_tree().paused = false
	get_tree().change_scene("res://scene/Main.tscn")
	pass


func _on_Button_pressed():
	if nengliangdou >0:
		nengliangdou -= 1
		use_nld = true
		$nld_number.text = str(nengliangdou)
	pass # Replace with function body.
